package com.simen.hellobytes.http;

import com.simen.hellobytes.StringUtil;

import java.util.ArrayList;
import java.util.List;

public class HttpCookie {

    public static List<HttpCookie> parse(String cookies) {
        List<HttpCookie> cookieList = new ArrayList<>();
        if (StringUtil.isTrimNotEmpty(cookies)) {
            String[] cookieArray = cookies.split(";");
            for (int i = 0; i < cookieArray.length; i++) {
                String cookie = cookieArray[i].trim();

                int equalIndex = cookie.indexOf("=");
                if (equalIndex != -1) {
                    HttpCookie httpCookie = new HttpCookie();
                    httpCookie.setName(cookie.substring(0, equalIndex));
                    httpCookie.setValue(cookie.substring(equalIndex + 1));

                    cookieList.add(httpCookie);
                }
            }
        }

        return cookieList;
    }

    private String name;
    private String value;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}
